<?php
	class userProf
	{
		var $uid,$name,$pwd,$status,$level,$nickName,$sex,$email,$college,$pro,$grade,$class,$introduction,$tag,$competition;
		
		public function __construct($data = array())
		{
			if (isset($data['uid'])) $this->uid = (int)$data['uid'];
			if (isset($data['userName'])) $this->name = $data['userName'];
			if (isset($data['userPwd'])) $this->pwd = $data['userPwd'];
			if (isset($data['userStatus'])) $this->status = $data['userStatus'];
			if (isset($data['userLevel'])) $this->level = $data['userLevel'];
			if (isset($data['userNickname'])) $this->nickName = $data['userNickname'];
			if (isset($data['userSex'])) $this->sex = $data['userSex'];
			if (isset($data['userEmail'])) $this->email = $data['userEmail'];
			if (isset($data['userCollege'])) $this->college = $data['userCollege'];
			if (isset($data['userPro'])) $this->pro = $data['userPro'];
			if (isset($data['userGrade'])) $this->grade = $data['userGrade'];
			if (isset($data['userClass'])) $this->class = $data['userClass'];
			if (isset($data['userIntroduction'])) $this->introduction = $data['userIntroduction'];
			if (isset($data['userTag'])) $this->tag = $data['userTag'];
			if (isset($data['userCompetition'])) $this->competition = $data['userCompetition'];
		}
		
		public static function getUidbyUsername($userName)
		{
			$conn = new mysqlClass();
			$query = "SELECT uid FROM users WHERE userName='{$userName}';";
			$conn->query($query);
			if ($conn->row == 1)
			{
				$conn->fetch();
				return $conn->data['uid'];
			}
			else
			{
				return 0;
			}
		}
		
		public static function getUserByUid($uid)
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM users WHERE uid={$uid};";
			$conn->query($query);
			if ($conn->row == 0)
			{
				return NULL;
			}
			else 
			{
				$conn->fetch();
				return new userProf($conn->data);
			}
		}
		public static function getUserByUsername($userName)
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM users WHERE userName={$userName};";
			$conn->query($query);
			if ($conn->row == 0)
			{
				return NULL;
			}
			else 
			{
				$conn->fetch();
				return new userProf($conn->data);
			}
		}
		public static function getUsernameByUid($uid)
		{
			$conn = new mysqlClass();
			$query = "SELECT userName FROM users WHERE uid={$uid};";
			$conn->query($query);
			if ($conn->row == 0)
			{
				return NULL;
			}
			else 
			{
				$conn->fetch();
				return $conn->data['userName'];
			}
		}
		
		public static function userFocus($cid,$uid,$action)
		{
			$conn = new mysqlClass();
			$query = "INSERT INTO focus (cid,uid,status) VALUES ({$cid},{$uid},'{$action}');";
			return $conn->query($query);
		}

		public static function getUserMessageByUsername($userName)
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM message WHERE messageTo='{$userName}';";
			$conn->query($query);
			if ($conn->row == 0) 
			{
				return "NO";
			}
			else
			{
				return $conn->result;
			}
			
		}

		public static function getInTeam($uid,$tid)
		{
			$conn = new mysqlClass();
			$query = "SELECT teamMemberUid FROM team WHERE tid={$tid};";
			if (!$conn->query($query))
			{
				return FALSE;
			}
			else
			{
				$conn->fetch();
				$i = $conn->data['teamMemberUid'].",{$uid}";
				$query = "UPDATE team SET teamMemberUid='{$i}' WHERE tid={$tid};";
				return $conn->query($query);				
			}
		}
		
		public static function postMessage($from,$to,$title,$content)
		{
			if (getNewUidAndCheckName($to,FALSE) != 0)
			{
				return 1;
			}
			else 
			{
				$conn = new mysqlClass();
				$time = time();
				$query = "INSERT INTO message (messageFrom, messageTo, messageTime, messageTitle, messageContent) VALUES ('{$from}','{$to}','{$time}','{$title}','{$content}');";
				if ($conn->query($query))
				{
					return 0;
				}
				else
				{
					return 3;
				}
			}
		}
		
		public function create()
		{
			$conn = new mysqlClass();
			$time = time();
			$query = "INSERT INTO users (uid,userName,userPwd,userEmail,registeredTime) VALUES ({$this->uid},'{$this->name}','{$this->pwd}','{$this->email}','$time');";
			if ($conn->query($query))
			{
				$query = "UPDATE record SET value='{$this->uid}' WHERE name='userAmount';";
				$conn->query($query);
				return TRUE;
			}
			else
			{
				return FALSE;
			}
		}
		
		private function gradedScore($competition,$team)
		{
			$conn = new mysqlClass();
			$query = "SELECT score FROM graded WHERE competiton='{$competition}' AND team='{$team}';";
			$conn->query($query);
			$score = 0;
			while ($i = mysql_fetch_array($conn->result))
			{
				$score += $i['score'];
			}
			return $i;
		}
		
		private function refreshScore()
		{
			$sum = 0;
			$allComp = explode(',', $this->competition);
			foreach ($allComp as $i)
			{
				$theCompetition = explode('-', $i);
				$theScore = compProf::getCompetitionScore($theCompetition['0']);
				switch ($theCompetition[''])
				{
					case '一等奖':
						$theScore = $theScore * 3;
						break;
					case '二等奖':
						$theScore = $theScore * 2.5;
						break;
					case '一等奖':
						$theScore = $theScore * 2;
						break;
					default:
						{}
				}
				$theScore = $theScore * gradedScore($theCompetition['0'],$theCompetition['2']);
				$sum += $theScore;
			}
			return $sum;
		}
		
		public function update($score=FALSE)
		{
			$conn = new mysqlClass();
			if (!$score)
			{
				$query = "UPDATE users SET userNickname='{$this->nickName}', userSex='{$this->sex}', userEmail='{$this->email}', userCollege='{$this->college}', userPro='{$this->pro}', userGrade='{$this->grade}', userClass='{$this->class}', userTag='{$this->tag}', userIntroduction='{$this->introduction}' WHERE uid={$this->uid};";
				return $conn->query($query);
			}
			else
			{
				$score = $this->refreshScore();
				$query = "UPDATE users SET userNickname='{$this->nickName}', userSex='{$this->sex}', userEmail='{$this->email}', userCollege='{$this->college}', userCompetition='{$this->competition}', userScore='{$score}', userPro='{$this->pro}', userGrade='{$this->grade}', userClass='{$this->class}', userTag='{$this->tag}', userIntroduction='{$this->introduction}' WHERE uid={$this->uid};";
				return $conn->query($query);
			}
		}
		
		public function pwdEdit($curPwd,$newPwd)
		{
			$conn = new mysqlClass();
			$query = "SELECT userPwd FROM users WHERE uid={$this->uid};";
			$conn->query($query);
			$conn->fetch();
			$temp = md5($curPwd);
			if ($temp == $conn->data['userPwd'])
			{
				$temp = md5($newPwd);
				$query = "UPDATE users SET userPwd='{$temp}' WHERE uid={$this->uid};";
				if ($conn->query($query))
				{
					return 0;
				}
				else 
				{
					return 3;
				}
			}
			else
			{
				return 1;
			}
		}
		
		public function getUserFile($all = TRUE)
		{
			$conn = new mysqlClass();
			if ($all)
			{
				$query = "SELECT * FROM files WHERE fileOwner='{$this->uid}';";
			}
			else
			{
				$query = "SELECT * FROM files WHERE fileOwner='{$this->uid}' AND fileStatus='Shared';";
			}
			$conn->query($query);
			if ($conn->row == 0) 
			{
				return "NO";
			}
			else
			{
				return $conn->result;
			}
		}
		
		public function getUserAlbum($albumName = "NONE")
		{
			$conn = new mysqlClass();
			if ($albumName == "NONE")
			{
				$query = "SELECT * FROM albums WHERE albumOwner='{$this->uid}';";
			}
			else
			{
				$query = "SELECT * FROM albums WHERE albumOwner='{$this->uid}' AND albumName='{$albumName}';";
			}
			$conn->query($query);
			if ($conn->row == 0) 
			{
				return "NO";
			}
			else
			{
				return $conn->result;
			}
		}
		
		private function getNewAid()
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM record WHERE name='albumAmount';";
			$conn->query($query);
			$conn->fetch();
			$i = $conn->data['value'] + 1;
			$query = "UPDATE record SET value='{$i}' WHERE name='albumAmount';";
			$conn->query($query);
			return $i;
		}
		
		public function albumCreate($albumName)
		{
			$aid = $this->getNewAid();
			$conn = new mysqlClass();
			$query = "INSERT INTO albums (aid,albumName,albumOwner,albumStatus) VALUES ({$aid},'{$albumName}',{$this->uid},'Shared');";
			return $conn->query($query);
		}
		
		public function imageUpload($file, $albumName)
		{
			$image = new imageFile();
			if ($image->doUpload($file, $albumName))
			{
				return TRUE;
			}
			else
			{
				return FALSE;
			}
		}
	
		public function competitionJoin($cid)
		{
			$conn = new mysqlClass();
			$query = "INSERT INTO focus (cid,uid,status,teamName) VALUES ({$cid},{$this->uid},'join','');";
			return $conn->query($query);
		}
		
		public function competitionStatus($cid)
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM focus WHERE cid={$cid} AND uid={$this->uid};";
			$conn->query($query);
			if ($conn->row == 0)
			{
				return "NO";
			}
			else
			{
				$conn->fetch();
				return $conn->data;
			}
		}
	}
	
	class compProf
	{
		var $cid,$admin,$name,$description,$status,$level,$score;
		
		public function __construct($data = array())
		{
			if (isset($data['cid'])) $this->cid = (int)$data['cid'];
			if (isset($data['competitionAdmin'])) $this->admin = $data['competitionAdmin'];
			if (isset($data['competitionName'])) $this->name = $data['competitionName'];
			if (isset($data['competitionDescription'])) $this->description = $data['competitionDescription'];
			if (isset($data['competitionStatus'])) $this->status = $data['competitionStatus'];
			if (isset($data['competitionScore'])) $this->score = $data['competitionScore'];
			if (isset($data['competitionLevel'])) $this->level = $data['competitionLevel'];
		}
		
		private function getNewCid()
		{
			$conn = new mysqlClass();
			$query = "SELECT value FROM record WHERE name='competitionAmount';";
			$conn->query($query);
			$conn->fetch();
			return $conn->data['value'] + 1;
		}
		
		public static function getCompetitionScore($competitionName)
		{
			$conn = new mysqlClass();
			$query = "SELECT competitionScore FROM competitions WHERE competitionName='{$competitionName}';";
			$conn->query($query);
			if ($conn->row == 0)
			{
				return 0;
			}
			else
			{
				$conn->fetch();
				return $conn['competitionScore'];
			}
		}
		
		public function create()
		{
			$conn = new mysqlClass();
			$query = "SELECT competitionName FROM competitions WHERE competitionName='{$this->name}';";
			$conn->query($query);
			if ($conn->row >= 1)
			{
				return FALSE;
			}
			else
			{
				$this->cid = $this->getNewCid();
				$query = "INSERT INTO competitions (cid,competitionName,competitionDescription,competitionAdmin,competitionLevel,competitionScore) VALUES ({$this->cid}, '{$this->name}', '{$this->description}', {$this->admin}, '{$this->level}', '{$this->score}');";
				if ($conn->query($query))
				{
					$query = "UPDATE record SET value='{$this->cid}' WHERE name='competitionAmount';";
					return $conn->query($query);
				}
				else 
				{
					return FALSE;
				}
			}
		}
		
		public static function getCompetitionByCid($cid)
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM competitions WHERE cid={$cid};";
			$conn->query($query);
			$conn->fetch();
			return new compProf($conn->data);
		}
		
		public static function getCompetitionNameByCid($cid)
		{
			$conn = new mysqlClass();
			$query = "SELECT competitionName FROM competitions WHERE cid={$cid};";
			$conn->query($query);
			$conn->fetch();
			return $conn->data['competitionName'];
		}
		
		public static function getAdminByCid($cid)
		{
			$conn = new mysqlClass();
			$query = "SELECT competitionAdmin FROM competitions WHERE cid={$cid};";
			$conn->query($query);
			$conn->fetch();
			return $conn->data['competitionAdmin'];
		}
		
		public static function getCompetitionTeamByCid($cid)
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM team WHERE teamCompetition={$cid};";
			$conn->query($query);
			if ($conn->row == 0)
			{
				return 'NO';
			}
			else
			{
				return $conn->result;
			}
		}
		
		public function update()
		{
			$conn = new mysqlClass();
			$query = "UPDATE competitions SET competitionName='{$this->name}', competitionDescription='{$this->description}' WHERE cid={$this->cid};";
			return $conn->query($query);
		}
		
		public function getCompetitionImage()
		{
			$person = userProf::getUserByUid((int)$this->admin);
			$result = $person->getUserAlbum($this->name);
			if ($result == "NO")
			{
				return "NO";
			}
			$result = mysql_fetch_array($result);
			return imageFile::getImageByAid($result['aid']);
			
		}
		
		public function readableStatus()
		{
			if ($this->status == "active")
			{
				return "进行中";
			}
			else if ($this->status == "end")
			{
				return "已结束";
			}
			else if ($this->status == "prepare")
			{
				return "准备中";
			}
			else
			{
				return "未知";
			}
		}
	
		public function getCompetitionPost($status = 'open')
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM posts WHERE postGroup='{$this->name}' AND postStatus='{$status}';";
			$conn->query($query);
			if ($conn->row == 0) 
			{
				return 'NO';
			}
			else
			{
				return $conn->result;
			}
		}
	}
	
	class teamProf
	{
		var $tid,$name,$leader,$member,$status,$level,$introduction,$competition;
		
		public function __construct($data = array())
		{
			if (isset($data['tid'])) $this->tid = $data['tid'];
			if (isset($data['teamName'])) $this->name = $data['teamName'];
			if (isset($data['teamLeaderUid'])) $this->leader = $data['teamLeaderUid'];
			if (isset($data['teamMemberUid'])) $this->member = $data['teamMemberUid'];
			if (isset($data['teamStatus'])) $this->status = $data['teamStatus'];
			if (isset($data['teamLevel'])) $this->level = $data['teamLevel'];
			if (isset($data['teamIntroduction'])) $this->introduction = $data['teamIntroduction'];
			if (isset($data['teamCompetition'])) $this->competition = $data['teamCompetition'];
		}
		
		public static function getTeamByTid($tid)
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM team WHERE tid={$tid};";
			$conn->query($query);
			$conn->fetch();
			return new teamProf($conn->data); 
		}
		
		public static function getTeamnameByTid($tid)
		{
			$conn = new mysqlClass();
			$query = "SELECT teamName FROM team WHERE tid={$tid};";
			$conn->query($query);
			$conn->fetch();
			return $conn->data['teamName']; 
		}

		public static function requestTeamInvite($tid,$userName)
		{
			$conn = new mysqlClass();
			$query = "SELECT action FROM rancode WHERE action='requestInTeam={$tid}&username={$userName}';";
			$conn->query($query);
			if ($conn->row != 0)
			{
				return 1;
			}
			else
			{
				$code = md5($tid.$userName);
				$query = "INSERT INTO rancode (action,code) VALUES ('requestInTeam={$tid}&username={$userName}','{$code}');";
				if ($conn->query($query))
				{
					$team = teamProf::getTeamByTid($tid);
					userProf::postMessage('Admin', userProf::getUsernameByUid($team->leader), '入队申请', "有用户申请加入你领导下的队伍{$team->name}, 点击<a href=\"?p=editTeam&do=memberManage&tid={$team->tid}\">这里</a>查看具体信息.");
					return 0;
				}
				else
				{
					return 2;
				}
			}
		}
		
		public static function chkTeamCode($code)
		{
			$conn = new mysqlClass();
			$query = "SELECT action FROM rancode WHERE code='{$code}';";
			$conn->query($query);
			$conn->fetch();
			if (strpos($conn->data['action'],'teamInvite') !== FALSE)
			{
				$arr['tid'] = substr($conn->data['action'], 11);
				if (userProf::getInTeam($_SESSION['uid'], $arr['tid']))
				{
					$arr['status'] = 'YES';
					$query = "DELETE FROM rancode WHERE code='${code}';";
					$conn->query($query);
				}
				else
				{
					$arr['status'] = 'NO';
				}
				return $arr;
			}
			else
			{
				$arr['status'] = 'NO';
				return $arr;
			}
		}
		
		public static function createInviteCode($tid)
		{
			$code = md5(time().$tid);
			$conn = new mysqlClass();
			$query = "INSERT INTO rancode (action,code) VALUES ('teamInvite={$tid}','{$code}');";
			if ($conn->query($query))
			{
				return "<a href='?p=ranCode&action=teamInvite&code={$code}'>链接</a>";
			}
			else
			{
				return "NO";
			}
		}
		
		public function create()
		{
			$tid = $this->getNewTid();
			$conn = new mysqlClass();
			$query = "INSERT INTO team (tid,teamName,teamLeaderUid,teamCompetition) VALUES ({$tid},'{$this->name}',{$this->leader},'{$this->competition}');";
			if ($conn->query($query))
			{
				$query = "UPDATE record SET value='{$tid}' WHERE name='teamAmount';";
				return $conn->query($query);
			}
			else
			{
				return FALSE;
			}
			
		}

		public function update()
		{
			$conn = new mysqlClass();
			$query = "UPDATE team SET teamIntroduction='{$this->introduction}' WHERE tid={$this->tid};";
			return $conn->query($query);
		}
		
		private function getNewTid()
		{
			$conn = new mysqlClass();
			$query = "SELECT value FROM record WHERE name='teamAmount';";
			$conn->query($query);
			$conn->fetch();
			return $conn->data['value'] + 1;
		}
	}